<?php

declare(strict_types=1);

namespace Tool\Database;

/**
 * dsn.
 */
class DSN
{
    /**
     * host.
     */
    private string $host = 'localhost';

    /**
     * port.
     */
    private string $port = '3306';

    /**
     * database name.
     */
    private string $dbName = '';

    /**
     * charset.
     */
    private string $charset = 'utf8';

    public function __toString()
    {
        $dsn = ['mysql:'];
        if ($this->getHost()) {
            $dsn[] = 'host='.$this->getHost();
        }
        if ($this->getPort()) {
            $dsn[] = 'port='.$this->getPort();
        }
        if ($this->getDbName()) {
            $dsn[] = 'dbname='.$this->getDbName();
        }
        if ($this->getCharset()) {
            $dsn[] = 'charset='.$this->getCharset();
        }

        return implode(';', $dsn);
    }

    /**
     * 设置host.
     *
     * @return static
     */
    public function setHost(string $host)
    {
        $this->host = $host;

        return $this;
    }

    /**
     * 获取host.
     */
    public function getHost(): string
    {
        return $this->host;
    }

    /**
     * 设置port.
     *
     * @return static
     */
    public function setPort(string $port)
    {
        $this->port = $port;

        return $this;
    }

    /**
     * 获取port.
     */
    public function getPort(): string
    {
        return $this->port;
    }

    /**
     * 设置database name.
     *
     * @return static
     */
    public function setDbName(string $dbName)
    {
        $this->dbName = $dbName;

        return $this;
    }

    /**
     * 获取database name.
     */
    public function getDbName(): string
    {
        return $this->dbName;
    }

    /**
     * 设置charset.
     *
     * @return static
     */
    public function setCharset(string $charset)
    {
        $this->charset = $charset;

        return $this;
    }

    /**
     * 获取charset.
     */
    public function getCharset(): string
    {
        return $this->charset;
    }
}
